import { defineStore } from 'pinia';
import {
  getPointsOverview,
  getPointsRecords,
  getMaxDiscountAmount,
  checkPointsEnough
} from '../api/points.js';

export const usePointsStore = defineStore('points', {
  state: () => ({
    // 积分总览信息
    overview: {
      totalPoints: 0,      // 总积分
      availablePoints: 0,  // 可用积分
      usedPoints: 0,       // 已使用积分
      frozenPoints: 0      // 冻结积分
    },
    // 积分记录列表
    records: [],
    // 可抵扣的最大金额
    maxDiscountAmount: '0',
    // 分页信息
    pagination: {
      page: 1,
      pageSize: 10,
      total: 0
    },
    // 加载状态
    loading: {
      overview: false,
      records: false,
      discount: false
    }
  }),

  getters: {
    // 获取积分总览
    getPointsOverview: (state) => state.overview,
    // 获取可用积分
    getAvailablePoints: (state) => state.overview.availablePoints,
    // 获取积分记录
    getPointsRecords: (state) => state.records,
    // 获取最大抵扣金额
    getMaxDiscountAmount: (state) => state.maxDiscountAmount,
    // 获取分页信息
    getPagination: (state) => state.pagination,
    // 获取加载状态
    isLoading: (state) => (type) => state.loading[type]
  },

  actions: {
    // 加载积分总览
    async loadPointsOverview() {
      if (this.loading.overview) return;
      
      try {
        this.loading.overview = true;
        const res = await getPointsOverview();
        if (res.code === 200) {
          this.overview = {
            ...this.overview,
            ...res.data
          };
        }
        return res;
      } catch (error) {
        throw error;
      } finally {
        this.loading.overview = false;
      }
    },

    // 加载积分记录
    async loadPointsRecords() {
      if (this.loading.records) return;
      
      try {
        this.loading.records = true;
        const res = await getPointsRecords();

        if (res.code === 200) {
          this.records = res.data;
        }
        return res;
      } catch (error) {
        throw error;
      } finally {
        this.loading.records = false;
      }
    },

    // 计算最大抵扣金额
    async calculateMaxDiscount() {
      if (this.loading.discount) return;
      
      try {
        this.loading.discount = true;
        const res = await getMaxDiscountAmount();
        if (res.code === 200) {
          this.maxDiscountAmount = res.data;
        }
        return res;
      } catch (error) {
        throw error;
      } finally {
        this.loading.discount = false;
      }
    },

    // 检查积分是否足够
    async checkPointsEnough(points) {
      try {
        const res = await checkPointsEnough(points);
        return res.code === 200 ? res.data : false;
      } catch (error) {
        throw error;
      }
    },

    // 刷新积分信息
    async refreshPointsInfo() {
      try {
        await Promise.all([
          this.loadPointsOverview(),
          this.calculateMaxDiscount()
        ]);
      } catch (error) {
        throw error;
      }
    },

    // 重置积分记录分页
    resetPagination() {
      this.pagination = {
        page: 1,
        pageSize: 10,
        total: 0
      };
      this.records = [];
    }
  }
});
